
package com.epfresh.supplier.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.OrderDetailAgent;

import multitype.ItemViewBinder;
import multitype.OnRecyclerViewItemClickListener;

public class OrderChildViewBinder
    extends ItemViewBinder<OrderDetailAgent.ContentBean.OrderListBean, OrderChildViewBinder.ViewHolder>  implements View.OnClickListener{

    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    @Override
    public void onClick(View v) {

        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer){
            int position = (int) tag;
            Object o = v.getTag(R.id.item_key_status);
            if (onRecyclerViewItemClickListener != null && o instanceof OrderDetailAgent.ContentBean.OrderListBean){
                onRecyclerViewItemClickListener.onItemClick(v,position,o);
            }
        }
    }

    public String packageUnit = "公斤";

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_order_detail_agent_child, parent, false);
        return new ViewHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull OrderDetailAgent.ContentBean.OrderListBean post) {
        holder.tv_item_order_child_id.setText("单号："+post.getOrderNo());
        holder.tv_item_order_child_time.setText(post.getCreateTime());
        holder.tv_item_order_child_person.setText(post.getBuyerManagerName()+"  "+post.getBuyerManagerPhone());
        holder.tv_item_order_child_number.setText(post.getProductCount()+packageUnit);
        if(post.getSumRefund()==null||"0".equals(post.getSumRefund())){
            holder.tv_minus_detail.setVisibility(View.GONE);
        }else {
            holder.tv_minus_detail.setVisibility(View.VISIBLE);
        }
        if(post.getButtonList()==null||post.getButtonList().size()==0){
            holder.tv_agent_child_code_sure.setVisibility(View.GONE);
        }else {
            holder.tv_agent_child_code_sure.setVisibility(View.VISIBLE);
            holder.tv_agent_child_code_sure.setTag(R.id.item_key_position, -1);
            holder.tv_agent_child_code_sure.setTag(R.id.item_key_status,post);
            holder.tv_agent_child_code_sure.setOnClickListener(this);
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_item_order_child_id,tv_item_order_child_time,tv_item_order_child_person,tv_item_order_child_number;
        private TextView tv_minus_detail,tv_agent_child_code_sure;
        ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_item_order_child_id = itemView.findViewById(R.id.tv_item_order_child_id);
            tv_item_order_child_time = itemView.findViewById(R.id.tv_item_order_child_time);
            tv_item_order_child_person = itemView.findViewById(R.id.tv_item_order_child_person);
            tv_item_order_child_number = itemView.findViewById(R.id.tv_item_order_child_number);
            tv_minus_detail = itemView.findViewById(R.id.tv_minus_detail);
            tv_agent_child_code_sure = itemView.findViewById(R.id.tv_agent_child_code_sure);
        }
    }
}
